package com.study.practice.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/")
public class TestStrReplace {

    private static final String Param = "\"%s\"";

    private static final String tmREG = "\"%s\":(\".*?\"|[a-zA-Z0-9]*)";

    private static final String replaceStr = "\"%s\":\"********\"";

    @RequestMapping("/")
    public String index() {
        System.out.println("greetings from spring boot==============");
        return "Greetings from Spring Boot!";
    }


    public void testCounter(String arg1) {
        System.out.println(arg1 + ",test counter ==============");
    }

    public static void main(String[] args) {
        JSONObject json = new JSONObject();
        json.put("name", "张三");
        json.put("sex", "女");
        json.put("address", "浙江杭州西湖转塘");
        JSONObject son = new JSONObject();
        son.put("name", "小明");
        son.put("sex", false);
        son.put("address", "浙江杭州萧山宁围");
        son.put("like", 1);
        json.put("son", son);
        System.out.println("原先：" + json.toJSONString());
        String aa = "\"like\":\"football\"";
        System.out.println(aa);
        String str = json.toJSONString();
        if (str.contains(String.format(Param, "like"))) {
            System.out.println("contain like");
            str = str.replaceAll(String.format(tmREG, "like"), String.format(replaceStr, "like"));
            System.out.println(str);
        }

        if (str.contains(String.format(Param, "sex"))) {
            System.out.println("contain sex");
            str = str.replaceAll(String.format(tmREG, "sex"), String.format(replaceStr, "sex"));
            System.out.println(str);
        }

        if (str.contains(String.format(Param, "name"))) {
            System.out.println("contain name");
            str = str.replaceAll(String.format(tmREG, "name"), String.format(replaceStr, "name"));
            System.out.println(str);
        }

    }
}
